angular.module('audit.controller',[])
    .controller('auditController', ['auditService', '$sce', '$scope',
        function (auditService, $sce, $scope) {
        console.log('audit');
        auditService.getPublishedAudits({},function (result) {
            if( result.data.length <= 0 ){
                return;
            }

            $scope.categories = [];
            //按照目录排序
            if( result.data.length > 1){
            result.data.sort(function(a,b){
                return a.category-b.category
                });
            }

            //组织审计数据
            var currentCategory = result.data[0].category;
            var category ={};
            category.name = result.data[0].name;
            category.id = result.data[0].category;
            var audits = [];

            for ( var i = 0; i < result.data.length; ++i){
                if((result.data[i].category != currentCategory)){
                    category.audits = audits;
                    $scope.categories.push(category);

                    currentCategory = result.data[i].category;
                    category = {};
                    category.name = result.data[i].name;
                    category.id = result.data[i].category;
                    audits=[];
                }
                var audit = result.data[i]; 
                audits.push(audit);               
            }
            category.audits = audits;
            $scope.categories.push(category);

        }, function (err) {
            console.error(err);
        });
      

        
    }])
    
    .controller('auditMoreController', ['auditService', '$sce', '$scope','$stateParams',
        function (auditService, $sce, $scope,$stateParams) {
        
        console.log('auditMore');

        auditService.getPublishedAudits({category:$stateParams.category},function (result) {
            if( result.data.length <= 0 ){
                return;
            }
            $scope.category = result.data[0].name;
            $scope.audits = result.data;
        }, function (err) {
            console.error(err);
        });
      

        
    }])
    ;
